import requests
import time
import tkinter as tk
import tkinter.messagebox
class APP:
    def __init__(self, width=500, height=250):
        self.w = width
        self.h = height
        self.title = '潭州课堂刷花助手'
        #软件名
        self.root = tk.Tk(className=self.title)
        #获取数据
        self.number = tk.IntVar()
        self.cd = tk.IntVar()
        self.roomid = tk.StringVar()
        self.token = tk.StringVar()

        #定义空间模块div
        frame_1 = tk.Frame(self.root)
        frame_2 = tk.Frame(self.root)
        frame_3 = tk.Frame(self.root)
        frame_4 = tk.Frame(self.root)
        frame_5 = tk.Frame(self.root)#发送按钮
        frame_6 = tk.Frame(self.root)
        frame_7 = tk.Frame(self.root)
        frame_8 = tk.Frame(self.root)

        #空间内容设置padx=10, pady=10
        number_label = tk.Label(frame_1, text='请输入刷花次数：',padx=10, pady=10)
        cd_label = tk.Label(frame_2, text='请输入冷却时间：',padx=10, pady=10)#，不得小于3秒
        roomid_label = tk.Label(frame_3, text='请输入roomid：', padx=10, pady=10)  # ，不得小于3秒
        token_label = tk.Label(frame_4, text='请输入token：', padx=10, pady=10)  # ，不得小于3秒

        #输入框声明
        number_entry = tk.Entry(frame_1, textvariable=self.number, highlightcolor='Fuchsia', highlightthickness=1, width=15)
        cd_entry = tk.Entry(frame_2, textvariable=self.cd, highlightcolor='Fuchsia', highlightthickness=1, width=15)
        roomid_entry = tk.Entry(frame_3, textvariable=self.roomid, highlightcolor='Fuchsia', highlightthickness=1, width=16)
        token_entry = tk.Entry(frame_4, textvariable=self.token, highlightcolor='Fuchsia', highlightthickness=1, width=17)
        #按钮
        play = tk.Button(frame_5, text='发送',fg='Purple', width=8,  command=self.flower)
        ps1 = tk.Label(frame_6, text='关于roomid与请输入token的获取，请前往直播间打')
        ps2 = tk.Label(frame_7, text='开F12--Network刷朵鲜花获取sendFlower文件提取')
        ps3 = tk.Label(frame_8, text='开发者：一叶孤舟----本软件仅供娱乐使用')

        #控件布局 显示
        frame_1.pack()
        frame_2.pack()
        frame_3.pack()
        frame_4.pack()
        frame_5.pack()
        frame_6.pack()
        frame_7.pack()
        frame_8.pack()

        #确定控件的位置 row行  column列
        number_label.grid(row=0, column=0)
        cd_label.grid(row=1, column=0)
        roomid_label.grid(row=2, column=0)
        token_label.grid(row=3, column=0)
        ps1.grid(row=6, column=0)
        ps2.grid(row=7, column=0)
        ps3.grid(row=8, column=0)

        number_entry.grid(row=0, column=1)
        cd_entry.grid(row=1, column=1)
        roomid_entry.grid(row=2, column=1)
        token_entry.grid(row=3, column=1)

        play.grid(row=4,column=2, ipadx=10, ipady=10)

    def flower(self):


        number = self.number.get()
        cd = self.cd.get()
        roomid = self.roomid.get()
        token = self.token.get()
        if number < 1:
            tkinter.messagebox.showerror('错误','刷花次数必须大于1')
        elif number == 1:
            if roomid:
                if token:
                    url = "https://open.shiguangkey.com/live/chatroom/sendFlower"
                    data = {"roomid": roomid}  # 156256488
                    headers = {"token": token}  # 3593fe7906363c5827c9496d87b5deb5
                    for i in range(number):
                        html = requests.post(url, headers=headers, data=data)
                        print(html)
                        time.sleep(cd)  # 冷却3秒
                        i += 1
                else:
                    tkinter.messagebox.showerror('错误', 'token参数必填')
            else:
                tkinter.messagebox.showerror('错误', 'roomid参数必填')
        elif number >= 2:
            if cd <3:
                tkinter.messagebox.showerror('错误', '冷却时间不能小于3秒')
            else:
                if roomid:
                    if token:
                        url = "https://open.shiguangkey.com/live/chatroom/sendFlower"
                        data = {"roomid": roomid}  # 156256488
                        headers = {"token": token}  # 3593fe7906363c5827c9496d87b5deb5
                        for i in range(number):
                            html = requests.post(url, headers=headers, data=data)
                            print(html)
                            time.sleep(cd)  # 冷却3秒
                            i += 1
                    else:
                        tkinter.messagebox.showerror('错误', 'token参数必填')
                else:
                    tkinter.messagebox.showerror('错误', 'roomid参数必填')



    def loop(self):
        self.root.resizable(False, False)
        self.root.mainloop()

if __name__ == "__main__":
    app = APP()
    app.loop()
